/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jun 21, 2012  1:59:09 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : XfrOfOwnershipDetailServices.java
 * TODO
 */
package jpa.service.promis.sales;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.sales.VwSyncMktBankDeduRequ;
import jpa.entitys.promis.sales.XfrOfOwnershipDetail;
import jpa.entitys.promis.sales.XfrOfOwnershipDetailPK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class XfrOfOwnershipDetailServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(XfrOfOwnershipDetailServices.class);

	/**
	 * @param em
	 */
	public XfrOfOwnershipDetailServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}


	public List<XfrOfOwnershipDetail> findAll() throws Exception 
	{
		return super.ConvertToYoureObject(super.findAll("XfrOfOwnershipDetail.findAll"));
	}
	
	public XfrOfOwnershipDetail findById(XfrOfOwnershipDetailPK id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "XfrOfOwnershipDetail.findById"));				
	}	
	
		
	public List<XfrOfOwnershipDetail> QueryTransferOfOwnership(String company, String project, String voucherType, String voucherSeries, long voucherNumber, BigDecimal revisionNumber) throws Exception {
		try{
			
			StringBuilder _strQBuilder = new StringBuilder();
//			_strQBuilder.append(" SELECT c "); 
//			_strQBuilder.append(" FROM XFR_OF_OWNERSHIP_DETAILS c ");
//			_strQBuilder.append(" WHERE c.UTILITY_FLAG='Y' ");
//			_strQBuilder.append(" AND c.COMPANY = ").append(Utilitys.strSql(company));
//			_strQBuilder.append(" AND c.PROJECT = ").append(Utilitys.strSql(project));
//			_strQBuilder.append(" AND c.VOUCHER_TYPE = ").append(Utilitys.strSql(voucherType));
//			_strQBuilder.append(" AND c.VOUCHER_SERIES = ").append(Utilitys.strSql(vouchaseSeries));
//			_strQBuilder.append(" AND c.VOUCHER_NUMBER = ").append(vouchaseNumber);
//			_strQBuilder.append(" AND c.REVISION_NUMBER = 0 ");
//			_strQBuilder.append(" AND c.STATUS = ").append(Utilitys.strSql(status));
//			_strQBuilder.append(" AND c.FLAG_CHARGE_TYPE = 'TRANSFER' ");			
//			_strQBuilder.append(" ORDER BY c.COMPANY,c.PROJECT,c.VOUCHER_TYPE,c.VOUCHER_SERIES,C.REVISION_NUMBER ,c.VOUCHER_NUMBER,C.SEQ_NO ASC ");	
			
//			_strQBuilder.append(" SELECT c "); 
//			_strQBuilder.append(" FROM XfrOfOwnershipDetail c ");
//			_strQBuilder.append(" WHERE c.utilityFlag='Y' ");
//			_strQBuilder.append(" AND c.id.company = ").append(Utilitys.strSql(company));
//			_strQBuilder.append(" AND c.id.project = ").append(Utilitys.strSql(project));
//			_strQBuilder.append(" AND c.id.voucherType = ").append(Utilitys.strSql(voucherType));
//			_strQBuilder.append(" AND c.id.voucherSeries = ").append(Utilitys.strSql(vouchaseSeries));
//			_strQBuilder.append(" AND c.id.voucherNumber = ").append(vouchaseNumber);
//			_strQBuilder.append(" AND c.revisionNumber = 0 ");
//			_strQBuilder.append(" AND c.status = ").append(Utilitys.strSql(status));
//			_strQBuilder.append(" AND c.flagChargeType = 'TRANSFER' ");			
//			_strQBuilder.append(" ORDER BY c.company,c.project,c.voucherType,c.voucherSeries,c.voucherNumber,c.revisionNumber,c.seqNo ASC ");
//			log.info(_strQBuilder.toString());
//			
//			return getEm().createQuery(_strQBuilder.toString()).getResultList();
			
				Map<String,Object> mapWhereValue = new HashMap<String, Object>();				
				mapWhereValue.put("company", company);									//log.info("company = " + company);
				mapWhereValue.put("project", project);									//log.info("project = " + project);
				mapWhereValue.put("voucherType", voucherType);							//log.info("voucherType = " + voucherType);
				mapWhereValue.put("voucherSeries", voucherSeries);						//log.info("voucherSeries = " + voucherSeries);
				mapWhereValue.put("voucherNumber", voucherNumber);						//log.info("voucherNumber = " + voucherNumber);
				mapWhereValue.put("revisionNumber", revisionNumber);							//log.info("revisionNumber = " + 0);
				mapWhereValue.put("utilityFlag", "Y");									//log.info("utilityFlag = " + "Y");
				mapWhereValue.put("flagChargeType", "TRANSFER");						//log.info("flagChargeType = " + "TRANSFER");
				
				//mapWhereValue.put("status", status);									//log.info("status = " + status);
				
				return super.ConvertToYoureObject(super.find("XfrOfOwnershipDetail.findByDocumentNo", mapWhereValue));
			
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public XfrOfOwnershipDetail InsertedOrUpdated(XfrOfOwnershipDetail vlaue) throws Exception {
		XfrOfOwnershipDetail obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdatedOn()))
			{					
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public XfrOfOwnershipDetail Updated(XfrOfOwnershipDetail vlaue) throws Exception {
		XfrOfOwnershipDetail obj = vlaue;
		try{
			
			if(vlaue.getId() != null)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}				
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<XfrOfOwnershipDetail> Inserted(List<XfrOfOwnershipDetail> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(XfrOfOwnershipDetail obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdatedOn()))
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public XfrOfOwnershipDetail Inserted(XfrOfOwnershipDetail value) throws Exception {
		try{
			
			value.setSyncCreatedby(this.getUser());
			value.setSyncCreatedon(this.getCurrent_date());
			value.setSyncUpdatedby(this.getUser());
			value.setSyncUpdatedon(this.getCurrent_date());
			
			super.inserted(value);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return value;
	}
	
	public XfrOfOwnershipDetail updated(XfrOfOwnershipDetail value) throws Exception {
		try{
			
			value.setSyncCreatedby(this.getUser());
			value.setSyncCreatedon(this.getCurrent_date());
			value.setSyncUpdatedby(this.getUser());
			value.setSyncUpdatedon(this.getCurrent_date());
			
			super.updated(value);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return value;
	}
	
	public List<XfrOfOwnershipDetail> Updated(List<XfrOfOwnershipDetail> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(XfrOfOwnershipDetail obj : vlauelist)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				common_lst.add(obj);
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);			
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

	
}
